﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace RaceList.Admin.Relay
{
    public partial class AwardList : System.Web.UI.Page
    {
        ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Category_Bind();
                Data_Bind();
            }
        }
        protected void Category_Bind()
        {
            var cycles = db.Activity_Cycle.Where(s => s.isDel == false).Select(s => new { s.Id, s.title });
            foreach (var item in cycles)
            {
                ddlActivity.Items.Add(new ListItem(item.title, item.Id.ToString()));
            }
            ddlActivity.Items.Insert(0, new ListItem("所有活动", "all"));

            var sponsors = db.Activity_Sponsor.Where(s => s.isDel == false).Select(s => new { s.Id, s.Name });
            foreach (var item in sponsors)
            {
                ddlSponsor.Items.Add(new ListItem(item.Name, item.Id.ToString()));
            }
            ddlSponsor.Items.Insert(0, new ListItem("所有赞助商", "all"));
            ddlSponsor.Items.Insert(1, new ListItem("无赞助商", "null"));
        }
        protected void Data_Bind()
        {
            var list = db.Activity_Awards.Where(s => s.isDel == false);
            if (ddlActivity.SelectedValue != "all")
            {
                list = Queryable.Where(list, s => s.cycleId.ToString() == ddlActivity.SelectedValue);
            }

            if (ddlSponsor.SelectedValue != "all")
            {
                var sponsorId = CommonHelp.GetSponsor(ddlSponsor.SelectedValue);
                if (sponsorId == null)
                {
                    list = list.Where(s => s.sponsorId == null);
                }
                else
                {
                    list = list.Where(s => s.sponsorId == sponsorId);
                }
            }
            list = list.OrderByDescending(s => s.addTime);

            var result = list.Select(s => new
            {
                Id = s.Id,
                Name = s.name,
                SumCount = s.sumCount,
                DayCount = s.dayCount,
                AddTime = s.addTime,
                Status = s.status,
                UseredCount = s.useredCount,
                SponsorName = s.Activity_Sponsor.Name
            });
            gvList.DataSource = result;
            gvList.DataBind();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int? ID = Convert.ToInt32(e.CommandArgument.ToString());
            Activity_Awards award = db.Activity_Awards.SingleOrDefault(a => a.Id == ID);
            if (award == null)
            {
                this.Message.Text = "删除成功！";
                return;
            }
            string message = string.Empty;
            if (award != null)
            {
                try
                {
                    switch (e.CommandName)
                    {
                        case "btnDel":
                            award.isDel = true;
                            award.delTime = DateTime.Now;
                            message = "删除";
                            break;
                        case "CheckStatus":
                            award.status = award.status ? false : true;
                            message = "启用";
                            break;
                    }
                    db.SubmitChanges();
                    this.Message.Text = "操作成功";
                    //.AddOperatingLog("奖品", message, message + "了奖品：" + award.name);
                }
                catch (Exception ex)
                {
                    //.AddErrorLog(ex);
                    StringHelper.Alert(this, "操作失败！", string.Empty);
                    this.Message.Text = "操作失败，失败原因：" + ex.Message + "！详细原因请查看错误日志。";
                }
                Data_Bind();
            }
            else
            {
                StringHelper.Alert(this, "此信息不存在或可能被删除，请您刷新本页面后继续操作！", string.Empty);
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#EFF3FB'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
            }
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            Data_Bind();
        }

        protected void ddlActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            Data_Bind();
        }
    }
}